package cn.gemiman.daoyi.module.cms.service.appsitealbum;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.gemiman.daoyi.module.cms.controller.admin.appsitealbum.vo.*;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsitealbum.AppSiteAlbumDO;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;

import cn.gemiman.daoyi.module.cms.dal.mysql.appsitealbum.AppSiteAlbumMapper;

import static cn.gemiman.daoyi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gemiman.daoyi.module.cms.enums.ErrorCodeConstants.*;

/**
 * 站点相册 Service 实现类
 *
 * @author 道一源码
 */
@Service
@Validated
public class AppSiteAlbumServiceImpl implements AppSiteAlbumService {

    @Resource
    private AppSiteAlbumMapper appSiteAlbumMapper;

    @Override
    public Long createAppSiteAlbum(AppSiteAlbumSaveReqVO createReqVO) {
        // 插入
        AppSiteAlbumDO appSiteAlbum = BeanUtils.toBean(createReqVO, AppSiteAlbumDO.class);
        appSiteAlbumMapper.insert(appSiteAlbum);
        // 返回
        return appSiteAlbum.getId();
    }

    @Override
    public void updateAppSiteAlbum(AppSiteAlbumSaveReqVO updateReqVO) {
        // 校验存在
        validateAppSiteAlbumExists(updateReqVO.getId());
        // 更新
        AppSiteAlbumDO updateObj = BeanUtils.toBean(updateReqVO, AppSiteAlbumDO.class);
        appSiteAlbumMapper.updateById(updateObj);
    }

    @Override
    public void deleteAppSiteAlbum(Long id) {
        // 校验存在
        validateAppSiteAlbumExists(id);
        // 删除
        appSiteAlbumMapper.deleteById(id);
    }

    private void validateAppSiteAlbumExists(Long id) {
        if (appSiteAlbumMapper.selectById(id) == null) {
            throw exception(APP_SITE_ALBUM_NOT_EXISTS);
        }
    }

    @Override
    public AppSiteAlbumDO getAppSiteAlbum(Long id) {
        return appSiteAlbumMapper.selectById(id);
    }

    @Override
    public PageResult<AppSiteAlbumDO> getAppSiteAlbumPage(AppSiteAlbumPageReqVO pageReqVO) {
        return appSiteAlbumMapper.selectPage(pageReqVO);
    }

}